﻿using UnityEngine;
using System.Collections.Generic;

public class StartAreaScripts : MonoBehaviour
{
	public GameObject npcObject;
	public GameObject gameManager;

	float treshold = 5f;
	float spawnDelay;
	List<GameObject> npcList;
	public bool spawn;

	void Start()
	{
		npcList = new List<GameObject>();
		//spawnDelay = treshold + (float)(Random.Range(0, 3000) / 1000f);
		spawnDelay = 0;
		//Debug.Log(spawnDelay);
		spawn = true;
	}

	void Update()
	{
		spawnDelay -= Time.deltaTime;
		if(spawnDelay <= 0 && npcList.Count < 3)
		{
			SpawnNPC();
			spawnDelay = treshold + (float)(Random.Range(0, 3000) / 1000f);
		}
	}

	void SpawnNPC()
	{
		if(spawn)
		{
			GameObject npc = Instantiate(npcObject, this.transform.position, Quaternion.identity) as GameObject;
			npc.transform.parent = this.transform;
			foreach(GameObject i in npcList)
			{
				i.transform.position = new Vector3(i.transform.position.x - 1.5f, i.transform.position.y, 5);
			}
			npc.transform.position =  new Vector3(npc.transform.position.x + 1.5f, npc.transform.position.y, 5);
			npcList.Add(npc);
			gameManager.GetComponent<GameplayScript>().totalNPC += 1;
		}
	}

	public void RemoveNPC(GameObject param)
	{
		//Debug.Log(param);
		npcList.Remove(param);
	}

	public GameObject FirstNPC
	{
		get{return npcList[0];}
	}
}
